package com.example.litterpainterdemo.utils

import android.content.Context
import android.content.SharedPreferences
import androidx.core.content.edit
import java.lang.ref.WeakReference
import java.util.Spliterator

class SPUtils private constructor(){

    companion object{
        private lateinit var weakContext:WeakReference<Context>
        private var instance:SPUtils? = null
        private val sp:SharedPreferences by lazy {
            weakContext.get()!!.getSharedPreferences(Constants.fileName,Context.MODE_PRIVATE)
        }

        //暴露给外部，获取对象
        fun getInstance(context: Context):SPUtils{
            if(instance == null){
                synchronized(this){
                    if(instance == null){
                        instance = SPUtils()
                    }
                }
            }

            return instance!!.apply {
                weakContext = WeakReference(context)
            }
        }
    }

    var isFirst:Boolean = true
        set(value) {
            field = value
            sp.edit {
                putBoolean(Constants.welcome_name,value)
            }
        }
        get() {
            return sp.getBoolean(Constants.welcome_name,true)
        }


}